package com.itheima.reggie.common;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.6.0</version>
</dependency>
*/

public class SMSUtils {

    /**
     * 发送短信
     * @param phone 手机号
     * @param code 验证码
     */
    public static void sendSms(String phone, String code){

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5t8Z5bUqopDTNA1D363V", "17OBzlk1TKHN9oTdUfdU0mEtVuQ2Fs");

        IAcsClient client = new DefaultAcsClient(profile);


        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("阿里云短信测试");  //测试签名
        request.setTemplateCode("SMS_154950909");  //模板
        request.setPhoneNumbers(phone);   //测试手机号
        request.setTemplateParam("{\"code\":\"" + code + "\"}");  //验证码

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}